package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Auther：DONG on 2021/11/2
 **/
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Autowired
    private AdChannelMapper adChannelMapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //调用mybatisplus的api进行列表查询
        //1.参数校验
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.分页参数的校验
        dto.checkParam();

        Page<AdChannel> page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<AdChannel> adChannelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //使用工具类进行判断是否为空

        if (StringUtils.isNotBlank(dto.getName())) {
            adChannelLambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
//        int i =2/0;  错误测试  异常
        IPage<AdChannel> adChannelIPage = page(page, adChannelLambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) adChannelIPage.getTotal());
        pageResponseResult.setData(adChannelIPage.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
            //1.参数校验
            if (null==adChannel){
                return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            //2.参数填充
            adChannel.setCreatedTime(new Date());
            //3.调用接口实现数据库插入
        boolean saveResult = this.save(adChannel);
        if (!saveResult){
            return  ResponseResult.errorResult(AppHttpCodeEnum.SAVE_FILE);
        }
        //4.返回前台

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateAdChannel(AdChannel adChannel) {
        //1.参数校验
        if (null==adChannel||null== adChannel.getId()){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.填充参数  无
        //3.调用接口实现修改数据库
        boolean updateResult = this.updateById(adChannel);
        if (!updateResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FILE);
        }
        //4.返回前台
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验
        if (null== id){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel = this.getById(id);
        if (null==adChannel){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //如果状态有效不能删除
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        }
        //调用接口实现数据库的山粗
        boolean deleteResult = this.removeById(id);
        if (!deleteResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FILE);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
